package chapterSystem.client.interfaceDrawing.activities;

import chapterSystem.client.ChapterSystemService;
import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.model.entitiesInt.ChapterEvent;
import chapterSystem.client.model.entitiesInt.Report;
import chapterSystem.client.model.enums.ActivityType;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import java.util.Date;
import java.util.List;

/**
 * User: Hasp
 * Date: 06.05.12
 * Time: 2:15
 */
public class SeeOneDayEventsActivity extends Activity {
    private final List<ChapterEvent> events;
    private final Activity callback;
    private final Date myDate;

    protected SeeOneDayEventsActivity(final Brother brother, final ActivityType type,
                                      final List<ChapterEvent> events, final Activity callback, final Date myDate) {
        super(brother, type);
        this.events = events;
        this.callback = callback;
        this.myDate = myDate;
    }

    @Override
    public void drawActivity(final StackLayoutPanel workArea) {
        workArea.clear();
        VerticalPanel panel =  new VerticalPanel();
        panel.setWidth("100%");
        panel.setHeight("100%");
        panel.setBorderWidth(3);
        Button createEventButton = new Button("Create event");
        createEventButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                new CreateEventActivity(brother, ActivityType.EVENT, myDate, SeeOneDayEventsActivity.this).drawActivity(workArea);
            }
        });

        panel.add(createEventButton);

        for (final ChapterEvent event : events) {
            final HTML fullText = new HTML("<b>" + event.getTitle() + "</b><p>" + event.getDescription());
            final VerticalPanel midPanel = new VerticalPanel();
            final HorizontalPanel horMidPanel = new HorizontalPanel();

            final Button submitReportButton = new Button("Submit Report");
            submitReportButton.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent clickEvent) {
                    new SubmitReportActivity(brother, ActivityType.REPORT, event.getEventId(),
                            SeeOneDayEventsActivity.this).drawActivity(workArea);
                }
            });

            final Button readReportsButton = new Button("Read Reports");
            readReportsButton.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent clickEvent) {
                    ChapterSystemService.App.getInstance().getReportsByEvent(event.getEventId(), brother.getRankBattle(),
                            new AsyncCallback<List<Report>>() {
                                @Override
                                public void onFailure(Throwable caught) {
                                    Window.alert(caught.getMessage());
                                }

                                @Override
                                public void onSuccess(List<Report> result) {
                                    new SeeReportsActivity(brother, ActivityType.REPORT, "See reports about " + event.getTitle(),
                                            result, SeeOneDayEventsActivity.this).drawActivity(workArea);
                                }
                            });
                }
            });

            horMidPanel.add(submitReportButton);
            horMidPanel.add(readReportsButton);

            horMidPanel.setCellHorizontalAlignment(submitReportButton, HasAlignment.ALIGN_LEFT);
            horMidPanel.setCellHorizontalAlignment(readReportsButton, HasAlignment.ALIGN_LEFT);

            horMidPanel.setWidth("100%");
            midPanel.setWidth("100%");
            midPanel.setHeight("100%");
            midPanel.add(fullText);
            midPanel.add(horMidPanel);
            midPanel.setCellHeight(horMidPanel, "25px");

            panel.add(midPanel);
        }

        Button back = new Button("<< Back");
        back.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                callback.drawActivity(workArea);
            }
        });

        panel.add(back);

        panel.setCellHorizontalAlignment(back, HasAlignment.ALIGN_CENTER);
        panel.setCellHorizontalAlignment(createEventButton, HasAlignment.ALIGN_CENTER);
        workArea.add(new ScrollPanel(panel), new HTML("See Events at " + myDate.toString()), 4);
    }
}
